package com.itheima52.mobilesafe.db.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.itheima52.mobilesafe.bean.BlackNumberInfo;
import com.itheima52.mobilesafe.test.TestBlackNumberDao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.session.PlaybackState.CustomAction;
import android.os.SystemClock;

public class BlackNumberDao {
	private BlackNumberOpenHelper helper;
	private BlackNumberDao dao;

	public BlackNumberDao(Context context) {
		helper = new BlackNumberOpenHelper(context);

	}

	/**
	 * 
	 * @param number电话号码
	 * @param mode拦截模式
	 */
	public boolean add(String number, String mode) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("number", number);
		contentValues.put("mode", mode);
		long rowid = db.insert("blacknumber", null, contentValues);
		if (rowid == -1) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 
	 * @param number通过电话号码删除
	 */
	public boolean delete(String number) {
		SQLiteDatabase db = helper.getWritableDatabase();
		int rowNumber = db.delete("blacknumber", "number=?",
				new String[] { number });
		if (rowNumber == 0) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 
	 * @param number通过电话号码修改拦截模式
	 */
	public boolean changeNumberMode(String number, String mode) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("mode", mode);
		int rowNumber = db.update("blacknumber", contentValues, "number",
				new String[] { number });
		if (rowNumber == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 通过电话号码查找 返回黑名单拦截模式
	 */
	public String findNumber(String number) {
		String mode = "";
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("blacknumber", new String[] { mode },
				"number=?", new String[] { number }, null, null, null);
		if (cursor.moveToNext()) {
			mode = cursor.getString(0);

		}
		cursor.close();
		db.close();
		return mode;

	}

	public List<BlackNumberInfo> findAll() {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.query("blacknumber", new String[] { "number", "mode" }, null,
						null, null, null, null);
		List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		while (cursor.moveToNext()) {
			BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
			blackNumberInfo.setMode(cursor.getString(0));
			blackNumberInfo.setNumber(cursor.getString(1));
			blackNumberInfos.add(blackNumberInfo);
		}
		cursor.close();
		db.close();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// SystemClock.sleep(3000);
		return blackNumberInfos;

	}

	/**
	 * 分页加载数据
	 * 
	 * @param pageNumber表示当是哪一页
	 * @param pageSize表示每一页有多少条数据
	 * @return limit表示限制 offset表示跳过,从第几条开始
	 */
	public List<BlackNumberInfo> findPar(int pageNumber, int pageSize) {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select number,mode from blacknumber limit ? offset ?",
				new String[] { String.valueOf(pageSize),
						String.valueOf(pageSize * pageNumber) });
		ArrayList<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		while (cursor.moveToNext()) {
			BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
			blackNumberInfo.setMode(cursor.getString(1));
			blackNumberInfo.setNumber(cursor.getString(0));
			blackNumberInfos.add(blackNumberInfo);

		}
		cursor.close();
		db.close();
		return blackNumberInfos;

	}

	/**
	 * 分批加载数据
	 * 
	 * @param startIndex
	 *            开始的位置
	 * @param maxPage
	 *            每一页展示的最大的数据条目
	 * @return
	 */
	public List<BlackNumberInfo> findPar2(int startIndex, int maxPage) {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select number,mode from blacknumber limit ? offset ?",
				new String[] { String.valueOf(maxPage),
						String.valueOf(startIndex) });
		ArrayList<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		while (cursor.moveToNext()) {
			BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
			blackNumberInfo.setMode(cursor.getString(1));
			blackNumberInfo.setNumber(cursor.getString(0));
			blackNumberInfos.add(blackNumberInfo);

		}
		cursor.close();
		db.close();
		return blackNumberInfos;

	}

	/**
	 * 获取总的记录数
	 * 
	 * @return
	 */
	public int getTotalNumber() {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from blacknumber", null);
		cursor.moveToNext();
		int count = cursor.getInt(0);
		cursor.close();
		// db.close();
		return count;

	}

}
